package com.fitsoftwareprocess.mobilemap.bus;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.fitsoftwareprocess.mobimap.dto.Coordinate;

public class ShortestPathFinder {
	private MyWebClient _MyWebClient;
	private String _Url;

	public ShortestPathFinder(){
		_MyWebClient = new MyWebClient("http://tempuri.org/");
		_Url = "http://10.0.2.2:1609/Service1.asmx";
	}
	
	public ArrayList<Coordinate> FindShortestPath(Coordinate start, Coordinate end)
	{
		ArrayList<Coordinate> lstCoord = new ArrayList<Coordinate>();
		String methodName = "FindShortestPath";
		SoapObject req = _MyWebClient.GetSoapObject(methodName);
		
		SoapObject startPointSoapObject = _MyWebClient.GetSoapObject(methodName);
		startPointSoapObject.addProperty("Longitude", Double.toString(start.get_longitude()));
		startPointSoapObject.addProperty("Latitude", Double.toString(start.get_latitude()));
		PropertyInfo startPointPropertyInfo = new PropertyInfo();
		startPointPropertyInfo.setName("startPoint");
		startPointPropertyInfo.setValue(startPointSoapObject);
		req.addProperty(startPointPropertyInfo);	
		
		SoapObject endPointSoapObject = _MyWebClient.GetSoapObject(methodName);
		endPointSoapObject.addProperty("Longitude", Double.toString(end.get_longitude()));
		endPointSoapObject.addProperty("Latitude", Double.toString(end.get_latitude()));
		PropertyInfo endPointPropertyInfo = new PropertyInfo();
		endPointPropertyInfo.setName("endPoint");
		endPointPropertyInfo.setValue(endPointSoapObject);
		req.addProperty(endPointPropertyInfo);
		
		SoapSerializationEnvelope envelope = _MyWebClient.GetEnvelope(req);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		HttpTransportSE androidHttpTransport = new HttpTransportSE(_Url);
        androidHttpTransport.debug = true;
        
        try 
        {
        	//SoapPrimitive soapResult = _MyWebClient.MakeCall(_Url, envelope, _MyWebClient.getNameSpace(), methodName);
            androidHttpTransport.call(_MyWebClient.getNameSpace() + methodName, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            int intPropertyCount = response.getPropertyCount();
            for (int i = 0; i < intPropertyCount; i++) 
            {
                SoapObject responseChild = (SoapObject) response.getProperty(i);
                Coordinate coordinate = new Coordinate();
                if(responseChild.hasProperty("Longitude"))
                {
                	coordinate.set_longitude(Double.parseDouble(responseChild.getProperty("Longitude").toString()));
                }
                if(responseChild.hasProperty("Latitude"))
                {
                	coordinate.set_latitude(Double.parseDouble(responseChild.getProperty("Latitude").toString()));	
                }
                lstCoord.add(coordinate);
            }
        }catch(Exception ex)
        {
        	Log.d("aa", ex.getMessage());
        }
		
		return lstCoord; 
	}
}
